<template>
  <Teleport to="body">
    <Transition name="modal">
      <div
        v-if="appStore.categoryModalVisible"
        class="modal-mask"
        @click="appStore.toggleCategoryModal"
      >
        <div class="modal-container" @click.stop>
          <div class="modal-header">
            <h3>选择分类</h3>
            <button class="close-button" @click="appStore.toggleCategoryModal">
              <i class="iconfont icon-close"></i>
            </button>
          </div>
          <div class="modal-body">
            <ul class="category-list">
              <li
                v-for="cat in appStore.categories"
                :key="cat.id"
                class="category-item"
                :class="{ active: cat.id === appStore.activeCategoryId }"
                @click="onSelect(cat)"
              >
                <el-icon v-if="cat.icon" :size="30">
                  <component :is="cat.icon" />
                </el-icon>
                <span>{{ cat.name }}</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { useAppStore } from '@/stores/app'

const appStore = useAppStore()

function onSelect(category) {
  appStore.selectCategoryAndCloseModal(category.id)
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  display: flex;
  transition: opacity 0.3s ease;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.modal-container {
  width: 90%;
  max-width: 480px;
  margin: auto;
  padding: 1rem;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 1px solid var(--border-color);
}

/* Style for dark mode */
html.dark .modal-container {
  background-color: #1a1a1a;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.5rem 1rem 0.5rem;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.1rem;
  color: var(--text-color-secondary);
  font-weight: 500;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-color-secondary);
}

.modal-body {
  padding: 0 0.5rem;
}

.category-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  gap: 0.75rem;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: calc(25% - 0.5625rem);
  padding: 1.25rem 0.5rem;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  border: 1px solid var(--border-light);
  background-color: var(--bg-light);
}

.category-item .el-icon {
  font-size: 2rem;
  color: var(--text-color-secondary);
  transition: color 0.2s ease-in-out;
}

.category-item span {
  color: var(--text-color);
  font-weight: 500;
}

.category-item:hover {
  background-color: var(--bg-hover);
}

.category-item.active {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

.category-item.active .el-icon,
.category-item.active span {
  color: white;
}

.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}

.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.9);
}

/* 响应式适配 */
@media screen and (max-width: 768px) {
  .modal-container {
    width: 95%;
    max-width: none;
  }
  .category-item {
    width: calc(33.333% - 0.5rem);
    padding: 1rem 0.25rem;
  }

  .category-item .el-icon {
    font-size: 1.5rem;
  }

  .category-item span {
    font-size: 0.75rem;
  }
}
</style>
